package com.smart.system.domain;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import io.swagger.v3.oas.annotations.media.Schema;
import com.smart.common.annotation.Excel;
import lombok.Data;
import lombok.Builder;

import lombok.*;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 告警对象 alarm
 *
 * @author smart
 * @date 2024-12-16
 */
@Data
@Builder
@TableName("alarm")
@AllArgsConstructor
@NoArgsConstructor
public class Alarm{
    private static final long serialVersionUID = 1L;

    //人员
    @TableField(exist = false)
    public static final Integer EMPLOYEE = 0;

    //车辆
    @TableField(exist = false)
    public static final Integer CAR = 1;

    //桶
    @TableField(exist = false)
    public static final Integer BUCKET = 2;

    //箱
    @TableField(exist = false)
    public static final Integer BOX = 3;

    //站
    @TableField(exist = false)
    public static final Integer PLATFORM = 4;

    /** 主键 */
    @Schema(description ="${comment}")
    @TableField(value = "id",exist = true)
    @TableId(type = IdType.AUTO)
    private Long id;

    /** 0人员1车辆 */
    @Excel(name = "0人员1车辆")
    @Schema(description ="0人员1车辆2垃圾箱")
    @TableField(value = "type",exist = true)
    private Integer type;

    /** 人员/车辆id */
    @Excel(name = "人员/车辆id")
    @Schema(description ="人员imei/车辆id")
    @TableField(value = "to_id",exist = true)
    private Integer toId;

    /** 告警信息 */
    @Excel(name = "告警信息")
    @Schema(description ="告警信息")
    @TableField(value = "message",exist = true)
    private Integer message;

    /**阅读状态**/
    @Excel(name = "阅读状态")
    @Schema(description ="阅读状态0未读1已读")
    @TableField(value = "status",exist = true)
    private Integer status;

    /**阅读状态**/
    @Excel(name = "区域Id")
    @Schema(description ="区域Id")
    @TableField(value = "area_id",exist = true)
    private Integer areaId;


    public String getMessageValue(){
        return Message.toMessage(this.message);
    }
    public String getTypeValue(){
        switch (this.type){
            case 0:
                return "人员";
            case 1:
                return "车辆";
            case 2:
                return "桶";
            case 3:
                return "箱";
            case 4:
                return "站";
            default:
                return "";
        }
    }

    /** 创建时间 */
    @Excel(name = "创建时间")
    @Schema(description ="创建时间")
    @TableField(value = "create_time",exist = true)
    private Date createTime;

    /** json */
    @Excel(name = "json_str")
    @Schema(description ="json")
    @TableField(value = "json_str",exist = true)
    private String jsonStr;


    public static Alarm getByMessage(Message message,Integer type,Integer toId,Integer areaId){
        Alarm alarm = new Alarm();
        alarm.setMessage(message.getCode());
        alarm.setCreateTime(new Date());
        alarm.setType(type);
        alarm.setToId(toId);
        alarm.setAreaId(areaId);
        return alarm;
    }

    public static Alarm getByMessage(Message message,Integer type,Integer toId,Integer areaId,String jsonStr){
        Alarm alarm = new Alarm();
        alarm.setMessage(message.getCode());
        alarm.setCreateTime(new Date());
        alarm.setType(type);
        alarm.setToId(toId);
        alarm.setAreaId(areaId);
        alarm.setJsonStr(jsonStr);
        return alarm;
    }

    /**
     * 告警信息去重
     * @param alarms
     * @return
     */
    public static List<Alarm> distinct(List<Alarm> alarms) {
        // 使用 Stream API 去重
        Map<String, Alarm> distinctAlarms = alarms.stream()
                .collect(Collectors.toMap(
                        alarm -> alarm.getType() + "-" + alarm.getToId() + "-" + alarm.getMessage(),
                        alarm -> alarm,
                        (existing, replacement) -> existing // 保留第一个出现的记录
                ));
        // 获取去重后的列表
        List<Alarm> distinctAlarmList = distinctAlarms.values().stream()
                .sorted(Comparator.comparing(Alarm::getCreateTime).reversed()) // 按创建时间降序排序
                .collect(Collectors.toList());
        return distinctAlarmList;
    }

    /**
     * 获取每个告警信息列表
     * @return
     */
    public static Map<Integer,List<Alarm>> getList(List<Alarm> list){
        Map<Integer, List<Alarm>> map = new HashMap<>();
        List<Alarm> employee = new LinkedList<>();
        List<Alarm> car = new LinkedList<>();
        List<Alarm> bucket = new LinkedList<>();
        List<Alarm> box = new LinkedList<>();
        List<Alarm> platform = new LinkedList<>();
        for (Alarm alarm : list) {
            if (EMPLOYEE.equals(alarm.getType())) {
                employee.add(alarm);
            }
            if (CAR.equals(alarm.getType())) {
                car.add(alarm);
            }
            if (BUCKET.equals(alarm.getType())) {
                bucket.add(alarm);
            }
            if (BOX.equals(alarm.getType())) {
                box.add(alarm);
            }
            if (PLATFORM.equals(alarm.getType())) {
                platform.add(alarm);
            }
        }
        map.put(EMPLOYEE,distinct(employee));
        map.put(CAR,distinct(car));
        map.put(BUCKET,distinct(bucket));
        map.put(BOX,distinct(box));
        map.put(PLATFORM,distinct(platform));
        return map;
    }
    @Getter
    public enum Message{
        //离开区域告警
        OUT_OF_AREA(1,"上班时间离开区域"),
        NO_CARD_PUNCHED(2,"上班时间未打卡"),
        LOW_CHARGE(3,"手表电量过低"),
        THE_OIL_IS_TOO_LOW(4,"油量过低"),
        REACH_THRESHOLD(5,"超出阈值"),
        OVER_SPEED(6,"超速"),
        SOS_ALARM(7, "SOS用户主动报警"),
        LOW_BATTERY(8, "手表身体状况警告"),
        BLOOD_PRESSURE_ALARM(9,"血压异常"),
        BLOOD_OXYGEN_ALARM(10,"血氧异常"),
        HEART_RATE_ALARM(11,"心率异常"),
        TEMPERATURE_ALARM(25,"体温异常");





        int code;
        String message;

        Message(int code, String message) {
            this.code = code;
            this.message = message;
        }
        //codemessage互转
        public static String toMessage(int code){
            for (Message item : Message.values()) {
                if(item.code==code){
                    return item.message;
                }
            }
            return null;
        }
        public static int toCode(String message){
            for (Message item : Message.values()) {
                if(item.message.equals(message)){
                    return item.code;
                }
            }
            return 0;
        }
    }
}
